//公共服务api接口
import 'package:xvideos/hj_model/home/video_list_resp.dart';
import 'package:xvideos/hj_utils/http_manager.dart';
import 'package:xvideos/tools_base/event_bus/events.dart';

import '../../alert_business/video/recommend_video_res.dart';
import '../../hj_model/home/home_tabs_entity.dart';
import '../../hj_model/message/services_model.dart';
import '../../hj_model/mine/follow_user_list_model.dart';
import '../../hj_model/mine/happy/HappyModel.dart';
import '../../hj_model/mine/task_center_data.dart';
import '../../hj_model/splash/domain_source_model.dart';
import '../../tools_base/event_bus/event_bus_util.dart';

class CommonService {
  /// 获取远端配置
  static Future<DomainSourceModel> fetchRemoteConfig() async {
    final result = await httpManager.fetchResponseByGET(
      '/ping/domain',
      jsonTransformation: (json) => DomainSourceModel.fromJson(json),
    );
    return result.data;
  }

  // //獲取客服信息
  static Future<ServicesModel?> getServices() async {
    final result = await httpManager.fetchResponseByGET(
      '/im/newSign',
      param: {},
      jsonTransformation: (json) => ServicesModel.fromJson(json),
    );
    return result.data;
  }

  //获取模块数据
  static Future<HomePlateModel?> getTagsMarkList() async {
    final result = await httpManager.fetchResponseByGET(
      '/modules/list',
      param: {},
      jsonTransformation: (json) => HomePlateModel.fromJson(json),
    );
    return result.data;
  }

  /// 查看模块专题是否付费过
  /// [id] 模块或则专题id
  /// [type] 专题1 模块0
  static Future<ModuleConfig?> checkModulesOrSectionConfig(String id, {int type = 0}) async {
    final result = await httpManager.fetchResponseByPOST(
      '/modules/check',
      param: {'mid': id, 'type': type},
      jsonTransformation: (json) => ModuleConfig.fromJson(json),
    );
    return result.data;
  }

  //发起点赞
  //SP:长视频 SHORT:短视频 COVER:图文帖子 PIC:图集帖子 SEED_LINK:种子/黄油帖子 TAG:标签 COMMENT:评论 video:动漫 image:漫画 text:小说
  static Future<bool> sendLike(String? objID, String? type) async {
    final param = {'objID': objID, 'type': type};
    final result = await httpManager.fetchResponseByPOST(
      '/thumbsUp',
      param: param,
    );
    eventBus.emit(CollectStatusModel(id: objID, isLiked: true, type: type));
    return result.isSuccess;
  }

  //取消点赞
  //SP:长视频 SHORT:短视频 COVER:图文帖子 PIC:图集帖子 SEED_LINK:种子/黄油帖子 TAG:标签 COMMENT:评论 video:动漫 image:漫画 text:小说
  static Future<bool> cancelLike(String? objID, String? type, {List<String>? objIDArr}) async {
    assert((type != null && type.isNotEmpty), 'cancelLike type 不能为空');
    List<String> ids = [];
    if (objIDArr?.isNotEmpty == true) {
      ids.addAll(objIDArr!);
    } else {
      ids.add(objID ?? "");
    }
    final param = {'objIDs': ids, 'type': type};
    final result = await httpManager.fetchResponseByPOST(
      '/thumbsDown',
      param: param,
    );
    eventBus.emit(CollectStatusModel(id: objID, isLiked: false, type: type));
    return result.isSuccess;
  }

  static Future<bool> cancelLikeArr(List<String>? objIDs, String? type, {String? tagID}) async {
    final param = {'objIDs': objIDs, 'type': type, 'tagID': tagID};
    param.removeWhere((key, value) => value == null);
    final result = await httpManager.fetchResponseByPOST(
      '/batch/thumbsDown',
      param: param,
    );
    return result.isSuccess;
  }

  static Future<bool> cancelCollectArr(List<String>? objIDs, String? type, {bool isCollect = false}) async {
    final param = {'objIds': objIDs, 'type': type, 'isCollect': false};
    param.removeWhere((key, value) => value == null);
    final result = await httpManager.fetchResponseByPOST(
      '/mine/batchUnCollect',
      param: param,
    );
    return result.isSuccess;
  }

  static Future<bool> postDelArr(List<String>? objIDs) async {
    final param = {'ids': objIDs};
    param.removeWhere((key, value) => value == null);
    final result = await httpManager.fetchResponseByPOST(
      '/mine/collection/del',
      param: param,
    );
    return result.isSuccess;
  }

  /// 广告点击
  static Future<dynamic> clickADEvent(String id) async {
    assert(id.isNotEmpty, 'clickADEvent id 不能为空');
    final param = {'id': id};
    final result = await httpManager.fetchResponseByPOST(
      '/ads/click',
      param: param,
    );
    return result.isSuccess;
  }

  /// 获取我关注用户发的视频
  /// type 为0 获取用户所有动态， 1为获取用户全部视频
  static Future<RecommendListRes?> fetchMyFollowDynamicList({int page = 1, int size = 20, int? uid, int? type, String? newsType}) async {
    final param = <String, dynamic>{'pageSize': size, 'pageNumber': page, 'uid': uid, 'type': type, 'newsType': newsType};
    param.removeWhere((key, value) => value == null);
    final result = await httpManager.fetchResponseByGET('/follow/dynamics/list',
        param: param, jsonTransformation: (json) => RecommendListRes.fromJson(json));
    return result.data;
  }

  /// 获取我关注用户发的短视频
  /// type 为0 获取用户所有动态， 1为获取用户全部视频
  static Future<RecommendListRes?> fetchMyFollowShortList({int page = 1, int size = 20}) async {
    final param = <String, dynamic>{'pageSize': size, 'pageNumber': page};
    param.removeWhere((key, value) => value == null);
    final result = await httpManager.fetchResponseByGET('/vid/short/follow',
        param: param, jsonTransformation: (json) => RecommendListRes.fromJson(json));
    return result.data;
  }

  //获取关注列表
  static Future<FollowUserListModel?> getFollowUsers({
    required int pageNumber,
    required int pageSize,
    int? uid,
    int? followUserType,
  }) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'uid': uid,
      'type': followUserType,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET('/follow/list', param: param, jsonTransformation: (json) {
      return FollowUserListModel.fromJson(json);
    });
    return result.data;
  }

  //获取金主广告列表
  static Future<HappyModel?> happyList() async {
    final result = await httpManager.fetchResponseByGET(
      '/recreation/list',
      param: {},
      jsonTransformation: (json) => HappyModel.fromJson(json),
    );
    return result.data;
  }

  static Future<TaskCenterData?> getMyTaskList() async {
    final result = await httpManager.fetchResponseByGET(
      '/task/list',
      param: {},
      jsonTransformation: (json) => TaskCenterData.fromJson(json),
    );
    return result.data;
  }

  static Future<bool> doTask(String? taskId, int? type) async {
    final result = await httpManager.fetchResponseByPOST(
      '/task/do',
      param: {'taskId': taskId, 'type': type},
      jsonTransformation: (json) => TaskCenterData.fromJson(json),
    );
    return result.isSuccess;
  }

  //推荐视频列表
  static Future<VideoListResp?> getRecommendList({
    required int pageNumber,
    required int pageSize,
    required String? vid,
  }) async {
    final param = {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'vid': vid,
    };
    param.removeWhere((k, v) => v == null);
    final result = await httpManager.fetchResponseByGET('/vid/recommend/list', param: param, jsonTransformation: (json) {
      return VideoListResp.fromJson(json);
    });
    return result.data;
  }
}
